/**
 * 
 */
package ca.lscs.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import ca.lscs.dal.ChecklistDao;
import ca.lscs.model.AppUser;
import ca.lscs.model.Checklist;
import ca.lscs.model.UserType;
import ca.lscs.web.utils.SessionUtils;

/**
 * @author Steve Pennington
 *
 */
@Controller
@SessionAttributes(value = { SessionUtils.USER_SESSION_KEY }, types = { AppUser.class })
public class HomeController {
	
	@RequestMapping(value="/")
	public ModelAndView handleHome(@ModelAttribute(SessionUtils.USER_SESSION_KEY) AppUser user) {
		ChecklistDao checklistDao = ChecklistDao.getInstance();
		Map<String, Object> model = new HashMap<String, Object>();
		List<Checklist> checklists = null;
		
		if(user.getType() == UserType.MANAGER) {
			checklists  = checklistDao.getOpenByCreator(user);
		} else if(user.getType() == UserType.SURVEYOR) {
			checklists  = checklistDao.getOpenByAsignee(user);
		}
		
		model.put("user", user);
		model.put("checklists", checklists);
		
		return new ModelAndView("home", model);
	}
	
	@RequestMapping(value="/submitted")
	public ModelAndView handleSubmitted(@ModelAttribute(SessionUtils.USER_SESSION_KEY) AppUser user) {
		ChecklistDao checklistDao = ChecklistDao.getInstance();
		Map<String, Object> model = new HashMap<String, Object>();
		List<Checklist> checklists = null;
		
		if(user.getType() == UserType.MANAGER) {
			checklists  = checklistDao.getSubmittedByCreator(user);
		} else if(user.getType() == UserType.SURVEYOR) {
			checklists  = checklistDao.getSubmittedByAsignee(user);
		}
		
		model.put("user", user);
		model.put("checklists", checklists);
		
		return new ModelAndView("home", model);
	}
}
